package com.daxia.ogmall.web.controller.m;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.daxia.ogmall.common.MyJSONObject;
import com.daxia.ogmall.dto.CityDTO;
import com.daxia.ogmall.dto.api.CityAPIDTO;
import com.daxia.ogmall.dto.api.info.CityInfoAPIDTO;
import com.daxia.ogmall.support.MPage;

@Controller
@RequestMapping(value = "/m/city", produces = "application/json;charset=UTF-8")
public class MCityController extends MBaseController {
    
    @ResponseBody
    @RequestMapping("list")
    public String list(CityDTO dto, MPage page) throws Exception {
    
        List<CityDTO> cityDTOs = cityService.find(dto, page);

        return toApiOutput(cityDTOs, CityAPIDTO.class, CityInfoAPIDTO.class, page);
    }
    
    @ResponseBody
    @RequestMapping("letter")
    public String letter() throws Exception {
    	MyJSONObject data = new MyJSONObject();
    	JSONArray letters = new JSONArray();
    	Map<String, List<CityDTO>> map = cityService.ListByLetter();
    	for (String key : map.keySet()) {
    		MyJSONObject letter = new MyJSONObject();
    		JSONArray cityInfos = new JSONArray();
    		List<CityDTO> list = map.get(key);
    		for (CityDTO dto : list) {
    			MyJSONObject obj = new MyJSONObject();
    			obj.put("id", dto.getId());
    			obj.put("name", dto.getName());
    			obj.put("letter", dto.getLetter());
    			obj.put("hot", dto.getIsHot());
    			String hotRemark = "非热点城市";
    			if (dto.getIsHot()) {
    				hotRemark = "热点城市";
    			}
				obj.put("hotRemark", hotRemark );
    			obj.put("fullLetter", dto.getFullletter());
    			cityInfos.add(obj);
    		}
    		letter.put(key, cityInfos);
    		letters.add(letter);
    		
    	}
    	data.put("cityInfos", letters);
    	return toJson(data);
    }
    
}
